package com.xiaochong.bitong.job.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaochong.bitong.job.config.hbase.HbaseBaseQueryComponent;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.mapper.SummaryMapper;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.po.Summary;
import com.xiaochong.tir.common.data.vo.app.AppWeightInfo;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.DateUtils;
import com.xiaochong.tir.common.util.HbaseTableUtils;

/**
 * @author qzw
 * 2018年8月6日 下午7:59:40
 * @description 
 **/
@Service
public class WeightService {
	 
	 @Autowired
	 private CurrencyInfoMapper currencyInfoDao;
	 
	 @Autowired
	 private SummaryMapper summaryDao;
	 
	 @Autowired
	 private HbaseBaseQueryComponent hbaseBaseQueryComponent;

	/**
	 * 获取权重信息
	 * @return
	 */
	public AppWeightInfo weightInfo(){
		AppWeightInfo appWeightInfo = new AppWeightInfo();
		 //查询权重货币
		 CurrencyInfo c = new CurrencyInfo();
		 c.setEnableFlag(true);
		 c.setDeleteFlag(false);
		 c.setWeights(true);
		 List<CurrencyInfo> currencyInfoList = currencyInfoDao.selectByEntityList(c);
		 try {
			 BigDecimal volume_24h = new BigDecimal(0);
			 BigDecimal market_value = new BigDecimal(0);
			 int rise = 0;
			 int fall = 0;
			 BigDecimal turnover_rate = new BigDecimal(0);
			 int maxKey = 0;
			 for(int i = 0; i < currencyInfoList.size(); i++){
				 CurrencyInfo ci = currencyInfoList.get(i);
				 //循环获取权重货币“每日货币信息”表数据
				 String data = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.DAILY_CURRENCY_INFOR_TABLE_NAME
						 , HbaseTableUtils.DAILY_CURRENCY_INFOR_TABLE_FAMILY
						 , HbaseTableUtils.DAILY_CURRENCY_INFOR_TABLE_QUALIFIER
						 , ci.getCmcId() + "@" + DateUtils.getCurrentDateString());
				 if(data != null && !"".equals(data.trim())){
					 JSONObject jobj = JSON.parseObject(data);
					 for(String key : jobj.keySet()){
						 if(Integer.valueOf(key) > maxKey){
							 maxKey = Integer.valueOf(key);
						 }
					 }
					 JSONArray array =jobj.getJSONArray(maxKey + "");
					 Long time = Long.valueOf(((JSONObject)array.get(0)).getString("createTime"));
					 JSONObject jo = (JSONObject)array.get(0);
					 //获取时间最靠前当前时间的一条数据
					 for(int j = 0; j < array.size(); j++){
						 JSONObject obj = (JSONObject) array.get(j);
						 if(time < Long.valueOf((obj.getString("createTime")))){
							 time = Long.valueOf((obj.getString("createTime")));
							 jo = obj;
						 }
					 }
					 //累加24小时交易额
					 volume_24h = volume_24h.add(new BigDecimal(jo.getString("volume24hCny")));
					 //累加市值
					 market_value = market_value.add(new BigDecimal(jo.getString("marketCapCny")));
					 //统计涨跌货币数量
					 if(jo.getString("percentChange24h").contains("-")){
						 fall = fall +1;
					 }else{
						 rise = rise +1;
					 }
					 //计算24小时换手率
					 turnover_rate = volume_24h.divide(market_value, 4,BigDecimal.ROUND_DOWN);
					 appWeightInfo.setFall(fall);
					 appWeightInfo.setRise(rise);
					 appWeightInfo.setVolume_24h(volume_24h);
					 appWeightInfo.setMarket_value(market_value);
					 appWeightInfo.setTurnover_rate(turnover_rate.multiply(new BigDecimal(100)));
					 //获取摘要
					 Summary t = new Summary();
					 t.setType("2");
					 t = summaryDao.selectByEntity(t);
					 appWeightInfo.setSummery(t.getContent());
				 }
			 }
		} catch (HbaseConnectionExcetion | HbaseCloseExcetion | HbaseGetTableExcetion | IOException e) {
			e.printStackTrace();
		} 
		return appWeightInfo;
	}
	
}
